from datetime import date, datetime, timedelta

class rule:
    def __init__(self, year=None, month=None, day=None, w=None, holidayfunction = None, functionoffset = None):
        self.year   = year
        self.month  = month
        self.day    = day
        self.w      = w
        self.function = holidayfunction
        self.offset = functionoffset
        self.date = None
        
    def __eq__(self, other):
            y = other.year
            m = other.month
            d = other.day
            w = other.isoweekday()
            if self.function is not None:     #check if rule is defined by function
                self.date = self.function(y)
                if self.offset is not None:
                    self.date = self.date + timedelta(self.offset)
                self.year = self.date.year
                self.month = self.date.month
                self.day = self.date.day
                self.w = self.date.isoweekday()
        
            if self.year is None:
                y = None
            
            if self.month is None:
                m = None
            
            if self.day is None:
                d = None
            
            if self.w is None:
                w = None
                
            rule_tuple = (self.year, self.month, self.day, self.w)
            date_tuple = (y,m,d,w)
            
            print rule_tuple, date_tuple
            
            return rule_tuple == date_tuple
        
    def __str__(self):
        return "Year: {}, Month: {}, Day: {}, Weekday: {}".format(self.year, self.month, self.day, self.w)
        
    def __hash__(self):
    # the hash of our string is our unique hash
        return hash(str(self))

class holiday:
    
    def __init__(self, name):
        self.rules = set()
        self.name = name
        
    def addRule(self, rule):
        self.rules.add(rule)
        

if __name__ == "__main__" :        
        #simplerule = rule(month = 12, day = 24, name = "Christmas Eve")
        mydate1 = datetime(2008, 12, 24)
        mydate2 = datetime(2008, 12, 22)
        #if simplerule == mydate1:
        #           print "{} is {}".format(mydate1, simplerule.name)
        #else:
         #          print "{} is not {}".format(mydate1, simplerule.name)
        #if simplerule == mydate2:
        #           print "{} is {}".format(mydate2, simplerule.name)
        #else:
        #           print "{} is not {}".format(mydate2, simplerule.name)
        
        simplerule = rule(month = 12, day = 24)
        print simplerule
        
        a = dict (Christmas=(rule(month = 12, day = 24), rule(month = 12, day = 23, w = 1)))
        print a
        
        simplerule = rule(month = 12, day = 24)
        print "Comparing {}".format(simplerule == mydate1)
        simplerule2 = rule(month = 12, day = 23, w = 1)
        h = holiday("Christmas")
        h.addRule(simplerule)
        h.addRule(simplerule2)
        print h
        print h.name
        for i in h.rules:
            print i
            print mydate1, mydate1 == i
            print mydate2, mydate2 == i
        
            
